package com.ccx.demo;

import com.alibaba.fastjson.JSON;
import com.ccx.demo.config.init.AppConfig;
import com.support.mvc.entity.base.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Objects;
import java.util.Properties;
import java.util.SortedMap;
import java.util.TreeMap;

import static com.alibaba.fastjson.serializer.SerializerFeature.PrettyFormat;
import static org.springframework.web.bind.annotation.RequestMethod.HEAD;
import static org.springframework.web.bind.annotation.RequestMethod.OPTIONS;

/**
 * <pre>
 * 参考配置：
 *   https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
 *   https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/
 *
 * @author 谢长春 2019/1/21
 */
@Slf4j
@SpringBootApplication
@EnableJpaAuditing
@EnableConfigurationProperties
public class MainApplication extends SpringBootServletInitializer {
    public static void main(String[] args) {
        final SpringApplication application = new SpringApplication(MainApplication.class);
        application.addListeners(
                (ApplicationListener<ApplicationEnvironmentPreparedEvent>) event -> {
                    final Properties properties = System.getProperties();
                    final SortedMap<String, Object> map = new TreeMap<>();
                    properties.forEach((key, value) -> map.put(Objects.toString(key), value));
                    log.info("系统环境变量：{}", JSON.toJSONString(map, PrettyFormat));
                }
//                (ApplicationListener<ApplicationContextInitializedEvent>) event -> {
//                },
//                (ApplicationListener<ApplicationPreparedEvent>) event -> {
//                }
        );
        application.run(args);
    }


    /**
     * @author 谢长春
     */
    @RequiredArgsConstructor
    @Controller
    public class AppController {

        private final AppConfig appConfig;

        /**
         * swagger 文档
         *
         * @return {@link String}
         */
        @GetMapping(value = "/")
        public String doc() {
            return "redirect:/doc.html";
        }

        /**
         * 用于获取 csrf token
         *
         * @return {@link String}
         */
        @RequestMapping(value = "/", method = {HEAD, OPTIONS})
        @ResponseBody
        public String csrf() {
            return "ok";
        }

        /**
         * 查看应用版本号
         *
         * @return {@link Result}
         */
        @GetMapping("/version")
        @ResponseBody
        public Result<String[]> version() {
            return new Result<String[]>().execute(result -> {
                result.setSuccess(new String[]{
                        appConfig.getVersion(),
                        appConfig.getIp(),
                        appConfig.getBuildTime(),
                        appConfig.getStartTime()
                });
            });
        }
//    public HomeController(ErrorAttributes errorAttributes) {
//        super(errorAttributes);
//    }

//    /**
//     * 处理服务端 500 异常
//     */
//    @RequestMapping(value = "/error", method = {GET, POST, PUT, PATCH, DELETE})
//    @ResponseBody
//    public Result<?> error() {
//        return Code.A00001.toResult("500：请求失败，不明确的异常");
//    }
    }
}
